package com.sskj.b70.v.activity;

import android.content.Intent;
import android.text.InputFilter;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.sskj.b70.MyAppLication;
import com.sskj.b70.R;
import com.sskj.b70.common.App;
import com.sskj.b70.common.Constants;
import com.sskj.b70.common.MoneyValueFilter;
import com.sskj.b70.common.widget.ToolBarLayout;
import com.sskj.b70.m.bean.CoinFeeBean;
import com.sskj.b70.m.bean.UserInfo;
import com.sskj.b70.p.activity.WithdrawPresenter;
import com.sskj.b70.utils.ClickUtil;
import com.sskj.b70.utils.NumberUtil;
import com.sskj.b70.utils.ToastUtil;
import com.sskj.b70.v.base.BaseActivity;

import java.util.concurrent.TimeUnit;

import butterknife.BindView;
import io.reactivex.Flowable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import io.reactivex.subscribers.DisposableSubscriber;

/**
 * <pre>
 *     USDT提币
 *     author : 吕志豪
 *     e-mail : 1030753080@qq.com
 *     time   : 2018/04/03
 *     desc   :
 *     version: 1.0
 * </pre>
 */

public class WithdrawActivity extends BaseActivity<WithdrawPresenter> {

    @BindView(R.id.et_address)  //地址
            EditText etAddress;
    @BindView(R.id.iv_address)
    ImageView ivAddress;
    @BindView(R.id.et_number)  // 出币数量
            EditText etNumber;
    @BindView(R.id.tv_all)
    TextView tvAll;
    @BindView(R.id.view_line)
    View viewLine;
    @BindView(R.id.et_password)  // 交易密码
            EditText etPassword;
    @BindView(R.id.bt_send_code)  // 发送验证码
            Button btSendCode;
    @BindView(R.id.et_check_code)  // 验证码
            EditText etCheckCode;
    @BindView(R.id.bt_withdraw) // 提币
            Button btWithdraw;
    @BindView(R.id.toolBar)
    ToolBarLayout mToolBar;
    @BindView(R.id.title)
    TextView mTitle;
    @BindView(R.id.tv_account)
    TextView mTvAccount;
    @BindView(R.id.tv_max_num)
    TextView mTvMaxNum;
    @BindView(R.id.tv_fee)
    TextView tvFee;
    @BindView(R.id.etIdCard)
    EditText etIdCard;
    @BindView(R.id.recharge_hint)
    TextView tvRechargeHint;
    @BindView(R.id.withdraw_unit)
    TextView tvWithdrawUnit;
    private DisposableSubscriber<Long> disposableSubscriber;
    private Double minCoin = 0d;
    private Double maxCoin = 0d;
    private Double wallone;
    private int mAssetId;
    private double mTb_fee;
    private String usableFund;
    private String assetName;
    private String idCard;


    @Override
    protected int getLayoutResId() {
        return R.layout.activity_withdraw;
    }

    @Override
    public WithdrawPresenter getPresenter() {
        return new WithdrawPresenter();
    }

    @Override
    protected void onViewBinding() {
        mAssetId = getIntent().getIntExtra(Constants.INTENT_ASSET_ID, -1);
        assetName = getIntent().getStringExtra(Constants.INTENT_ASSET_NAME);  // 类型---USDT
        usableFund = getIntent().getStringExtra("usableFund");  // 可用金额
        ClickUtil.click(btSendCode, () -> {
            mPresenter.sendCode();
        });

        ClickUtil.click(ivAddress, () -> {
            startActivityForResult(new Intent(this, CashAddressListActivity.class).putExtra(Constants.INTENT_CALL_BACK, true), Constants.CASH_ADDRESS);
        });

        mTitle.setText(assetName + App.INSTANCE.getString(R.string.strWithdrawActivity1));
        tvRechargeHint.setText(App.INSTANCE.getString(R.string.tip_notice) + assetName + App.INSTANCE.getString(R.string.strWithdrawActivity3) + assetName + App.INSTANCE.getString(R.string.strWithdrawActivity4));
        tvWithdrawUnit.setText(assetName);
//        mToolBar.setRightButtonIcon(getResources().getDrawable(R.mipmap.icon_bill));
        mToolBar.setLeftButtonOnClickLinster(v -> finish());
//        mToolBar.setRightButtonOnClickLinster(view -> startActivity(new Intent(mActivity, WithdrawRecordActivity.class)));
        ClickUtil.click(btWithdraw, () -> {
            if (TextUtils.isEmpty(etAddress.getText())) {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.strWithdrawActivity5));
                return;
            }
            if (TextUtils.isEmpty(etNumber.getText())) {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.strWithdrawActivity6));
                return;
            }
            Double aDouble = Double.valueOf(etNumber.getText().toString());
            if (aDouble < minCoin) {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.strWithdrawActivity7) + minCoin + App.INSTANCE.getString(R.string.strWithdrawActivity8));
                return;
            }

            if (aDouble > maxCoin) {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.strWithdrawActivity9) + maxCoin + App.INSTANCE.getString(R.string.strWithdrawActivity8));
                return;
            }

            if (aDouble > wallone - mTb_fee / 100) {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.strWithdrawActivity11));
                return;
            }
            if (TextUtils.isEmpty(etIdCard.getText())) {
                ToastUtil.showShort(getString(R.string.input_idcard));
                return;
            }
            if (!etIdCard.getText().toString().equalsIgnoreCase(idCard)) {
                ToastUtil.showShort(getString(R.string.idcard_error));
                return;
            }
            if (TextUtils.isEmpty(etPassword.getText())) {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.strWithdrawActivity12));
                return;
            }
            if (TextUtils.isEmpty(etCheckCode.getText())) {
                ToastUtil.showShort(getString(R.string.code_input));
                return;
            }
            // 提币接口调试   (id  出币数量  交易密码  验证码  地址  类型--USDT)
            mPresenter.withdraw(MyAppLication.getID(), etNumber.getText().toString().trim(),
                    etPassword.getText().toString().trim(),
                    etCheckCode.getText().toString().trim(),
                    etAddress.getText().toString().trim(), assetName);
        });
//        EditChangeUtil.onChange(etCheckCode, s -> {
//            if (s.length() == 6) {
//                mPresenter.checkCode(s.toString());
//            }
//        });
        etNumber.setFilters(new InputFilter[]{new MoneyValueFilter().setDigits(4)});
//        mPresenter.getUserInfo();
        mPresenter.getCoinFee(assetName);
        mPresenter.getUserInfo();

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK & requestCode == Constants.CASH_ADDRESS) {
            String address_url = data.getStringExtra("address_url");
            if (!TextUtils.isEmpty(address_url)) {
                etAddress.setText(address_url);
            }
        }
    }

    public void startCount() {
        btSendCode.setEnabled(false);
        disposableSubscriber = new DisposableSubscriber<Long>() {
            @Override
            public void onNext(Long aLong) {
                int time = (int) (60 - aLong);
                btSendCode.setText(time + App.INSTANCE.getString(R.string.strWithdrawActivity14));
            }

            @Override
            public void onError(Throwable t) {
                System.out.println(t.toString());
            }

            @Override
            public void onComplete() {
                btSendCode.setText(App.INSTANCE.getString(R.string.strWithdrawActivity15));
                btSendCode.setEnabled(true);
                if (!disposableSubscriber.isDisposed()) {
                    disposableSubscriber.dispose();
                    disposableSubscriber = null;
                }

            }
        };
        Flowable.interval(0, 1, TimeUnit.SECONDS, Schedulers.newThread())
                .take(60)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(disposableSubscriber);
    }

//    public void setCodeOk() {
//        btWithdraw.setEnabled(true);
//    }

    public void updateCoin(CoinFeeBean data) {
        wallone = Double.valueOf(usableFund);
        mTb_fee = data.getTb_fee();
        mTvMaxNum.setText(App.INSTANCE.getString(R.string.strWithdrawActivity16) + usableFund);
        ClickUtil.click(tvAll, () -> {
            etNumber.setText(String.valueOf(wallone - data.getTb_fee()));
        });
        minCoin = data.getTb_min();
        maxCoin = data.getTb_max();
        etNumber.setHint(App.INSTANCE.getString(R.string.strWithdrawActivity17) + data.getTb_min() + App.INSTANCE.getString(R.string.strWithdrawActivity18) + data.getTb_max());
        tvFee.setText(App.INSTANCE.getString(R.string.strWithdrawActivity19) + NumberUtil.keepMax4(data.getTb_fee()) + "%");
    }

    public void updateUserInfo(UserInfo data) {
        idCard = data.getIdcard();
    }
}
